**Calculate Age-adjustment for each country
import delimited "${output}\Country_Level_Summary.csv", clear
keep country_name age*
forval i=1/7 {
	sum age_group`i'
gen Gage_group`i'= r(mean)
	}
	
forval i=1/7 {
gen Age_Weight`i'=Gage_group`i'/age_group`i'
}

keep Age_W* country_name
reshape long Age_Weight, i(country_name) j(age_group_num)
save "${output}\age_balancing_weight.dta", replace

***********
*Findings 3: OLS--parent effect by country updated with country-age-weights
*******
use "${clean_data}\gfs_cleaned_merged_database.dta", clear
cd "${output}\"

gen age_group_num=1 if age_cat==18
replace age_group_num=2 if age_cat==25
replace age_group_num=3 if age_cat==35
replace age_group_num=4 if age_cat==45
replace age_group_num=5 if age_cat==55
replace age_group_num=6 if age_cat==65
replace age_group_num=7 if age_cat==75

*merge in age-weight
merge m:1 country_name age_group_num using "${output}\age_balancing_weight.dta"
drop _merge

encode country_name, gen(country_num)


gen B1=.
gen B2=.
gen P1=.
gen P2=.
gen SE1=.
gen SE2=.
gen DF1=.
gen DF2=.


forval i=1/22 {
reg HOPE PCRQ $demo living_structure2 living_structure3 living_structure4 [aw=Age_Weight] if country_num==`i'
replace B1=_b[PCRQ] if country_num==`i'
replace SE1=_se[PCRQ] if country_num==`i'
replace P1=_r_p[PCRQ] if country_num==`i'
replace DF1=_r_df[PCRQ] if country_num==`i'
}

forval i=1/22 {
reg PCRQ $demo parent_relig_index [aw=Age_Weight] if country_num==`i'
replace B2=_b[parent_relig_index] if country_num==`i'
replace SE2=_se[parent_relig_index] if country_num==`i'
replace P2=_r_p[parent_relig_index] if country_num==`i'
replace DF2=_r_df[parent_relig_index] if country_num==`i'
}

collapse (count) obs=PCRQ (first) country_name B1 B2 SE1 SE2 P1 P2 DF1 DF2 iso country_num v2x_polyarchy zwvs_traditional zwps_index lngdp zmort_rate ///
wps_education wps_employment wps_fin_inclusion wps_cell_phone wps_parliament wps_no_legal_discrimination wps_justice wps_mortality_ratio wps_son_bias wps_partner_violence wps_safety wps_terrorism wps_conflict ///
(mean) PCRQ thriving HOPE HAPPY ACCEPTED_BY_GOD FINANCIAL parent_relig_index self_relig_index parents_married parent_died  flove_no_dad mlove_no_mom child_poverty was_abused age [aw=weight], by(country)

gen T1=B1/SE1
gen T2=B2/SE2

foreach x in B1 B2 SE1 SE2 T1 T2 P1 P2 DF1 DF2 {
	ren `x' AGE`x'
}

save "country_level_effect_estimates_age_weighted.dta", replace

*Model results from main analysis file
merge 1:1 country using  "country_level_effect_estimates.dta"

cor B1 AGEB1 
cor B2 AGEB2

gsort -B1
order country age AGEB1 AGEB2 AGESE1 AGESE2  AGET1 AGET2 AGEP1 AGEP2 AGEDF1 AGEDF2 B1 B2 SE1 SE2 T1 T2

cor age AGEB1 AGEB2 AGESE1 AGESE2 AGET1 AGET2 AGEP1 AGEP2 AGEDF1 AGEDF2 B1 B2 SE1 SE2 T1 T2 PCRQ

sort age

export delimited country country_name age AGEB1 AGEB2 AGESE1 AGESE2 AGET1 AGET2 AGEP1 AGEP2 AGEDF1 AGEDF2 B1 B2 SE1 SE2 T1 T2 PCRQ P1 P2 DF1 DF2 ///
using "country_level_effect_estimates_age_weighted.csv", replace
